using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using MasterCSharp.WebServices ;

namespace WebUpdate
{
	/// <summary>
	/// Summary description for Service1.
	/// </summary>
	public class Update : System.Web.Services.WebService
	{
		public Update()
		{
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
		}

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;
				
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if(disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		#region old
		/*
		[WebMethod]
		public UpdateInfos IsUpdateCompressed(string version, string exename, System.DateTime askdt)
		{
			
			string filepath = Server.MapPath("Updates/" + version + "/" + exename );

			if ( System.IO.File.Exists( filepath ) )
			{
				
				System.DateTime dt = System.IO.File.GetLastWriteTimeUtc(filepath);
				if(dt > askdt)
				{
					return GetUpdateInfos( version + "/" + exename, dt.Year + "." + dt.Month + "." + dt.Day );
				}

				//else return no updates
				return null;

			}

			
			return null;


		}

		*/
		#endregion


		private UpdateInfos GetUpdateInfos(string filepath, string version)
		{
			UpdateInfos ui = new UpdateInfos();		

			//get the relativepath
			int start = filepath.IndexOf(version, 0, filepath.Length-1);
			string virtualpath = filepath.Substring(start+version.Length, (filepath.Length) - (start+version.Length) );
				
			ui.filepath = virtualpath;
			ui.time = System.IO.File.GetLastWriteTimeUtc( filepath );

			return ui;
		}

		private UpdateInfos GetUpdateInfosAndBinary(string filepath, string version)
		{
			UpdateInfos ui = new UpdateInfos();		
			System.IO.FileStream fs = System.IO.File.OpenRead(  filepath );
			
			//get the relativepath
			int start = filepath.IndexOf(version, 0, filepath.Length-1);
			string virtualpath = filepath.Substring(start+version.Length, (filepath.Length) - (start+version.Length) );
				
			ui.filepath = virtualpath;
			ui.bin = new byte[fs.Length];
			fs.Read( ui.bin, 0, (int)fs.Length );
			fs.Close();
			
			ui.time = System.IO.File.GetLastWriteTimeUtc( filepath );

			return ui;
		}


		private System.Collections.ArrayList GetUpdateInfosRecursive(string path, string version)
		{
			System.Collections.ArrayList ups = new ArrayList();

			string[] files = System.IO.Directory.GetFiles( path );
			string[] dirs = System.IO.Directory.GetDirectories( path );

			foreach ( string file in files )
			{
				ups.Add( GetUpdateInfos( file, version ) );
			}

			foreach ( string dir in dirs )
			{
				System.Collections.ArrayList internalresult = GetUpdateInfosRecursive( dir, version );
				foreach ( UpdateInfos u in internalresult )
				{
					ups.Add( u );
				}

			}

			return ups;

		}


		[WebMethod]
		[CompressionExtension]
		public System.Collections.ArrayList GetAllUpdateInfosCompressed(string version)
		{
			try
			{
				string path = Server.MapPath("Updates/" + version + "/");
			
				return GetUpdateInfosRecursive( path, version );
			}
			catch
			{
				return null;
			}

		}


		[WebMethod]
		[CompressionExtension]
		public UpdateInfos GetFileCompressed(string version, string fpath)
		{
			try
			{
				string filepath = Server.MapPath("Updates/" + version + "/" + fpath );

				if ( System.IO.File.Exists( filepath ) )
				{
					
					return GetUpdateInfosAndBinary( filepath, version );

				}

			}
			catch
			{
				return null;
			}

			
			return null;


		}


		[WebMethod]
		[CompressionExtension]
		public string UploadFileCompressed(string version, string fpath, byte[] content)
		{
			try
			{
				string filepath = Server.MapPath("Updates/" + version + "/" + fpath );

				if ( System.IO.File.Exists( filepath ) )
				{
					System.IO.File.Delete( filepath );
				}

				System.IO.FileStream fs = System.IO.File.Create( filepath );
				fs.Write( content, 0, content.Length );
				fs.Close();

			}
			catch(Exception ex)
			{
				return "Upload Failed : " + ex.Message + "\r\n" + ex.StackTrace ;
			}

			
			return "Upload done !";


		}


	}
}
